//
//  KLRealNameViewController.m
//  KunLun
//
//  Created by Mac on 2018/1/29.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "KLRealNameViewController.h"

@interface KLRealNameViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation KLRealNameViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"实名认证";
    [self configDataSouce];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configDataSouce{
    
    NSArray * sectionOne = @[
                             [[STTableEasyModel alloc] initWithTextString:@"姓名" detailString:self.userModel.name],
                             [[STTableEasyModel alloc] initWithTextString:@"身份证号" detailString:self.userModel.ID_card],
                             ];
    
    self.dataSouce = [sectionOne mutableCopy];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1  ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    
    
    //姓名
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self onSelectedRealNameCellWithModel:model indexPath:indexPath];
    }
    //身份证
    if (indexPath.section == 0 && indexPath.row == 1) {
        [self onSelectedIDCardCellWithModel:model indexPath:indexPath];
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
//真实姓名
- (void)onSelectedRealNameCellWithModel:(STTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:@"请输入姓名"
                                                       title:@"姓名"
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          if (text.length && ![text isEqualToString:model.detailString]) {
                                                              [self sendInfoChangeRequestWithRealName:text indexPath:indexPath];
                                                          }
                                                          if (!text.length) {
                                                              [SVProgressHUD showErrorWithStatus:@"姓名不能为空"];
                                                          }
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    detail.detailLabel.text = @"";
    detail.isShowTextNum = NO;
    detail.maxTextNum = 1000;
    [self.navigationController pushViewController:detail animated:YES];
}
//身份证
- (void)onSelectedIDCardCellWithModel:(STTableEasyModel*)model indexPath:(NSIndexPath*)indexPath{
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:@"请输入身份证号"
                                                       title:@"身份证号码"
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          if (text.length && ![text isEqualToString:model.detailString]) {
                                                              [self sendInfoChangeRequestWithIdCard:text indexPath:indexPath];
                                                          }
                                                          if (!text.length) {
                                                              [SVProgressHUD showErrorWithStatus:@"身份证号码不能为空"];
                                                          }
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    detail.detailLabel.text = @"请务必填写自己真实有效的身份证信息";
    detail.isShowTextNum = NO;
    detail.maxTextNum = 1000;
    detail.textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark --Private Method
- (NSDictionary*)nameDicWithNameValue:(NSString*)name{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"1" forKey:@"type"];
    if (name.length) {
        [dic setObject:name forKey:@"username"];
    }
    return dic;
}
- (NSDictionary*)dicWithIDCradValue:(NSString*)idCard{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@"2" forKey:@"type"];
    if (idCard.length) {
        [dic setObject:idCard forKey:@"id_card"];
    }
    return dic;
}

#pragma mark --NetWork Method
- (void)sendInfoChangeRequestWithRealName:(NSString*)name indexPath:(NSIndexPath*)indexPath{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (name.length) {
        [dic setObject:name forKey:@"name"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/updateInfo?token=",[STUserManger defult].token];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"更改姓名成功"];
                                                 STTableEasyModel * model = self.dataSouce[indexPath.row];
                                                 model.detailString = name;
                                                 [self.tableView reloadData];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendInfoChangeRequestWithIdCard:(NSString*)idCard indexPath:(NSIndexPath*)indexPath{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (idCard.length) {
        [dic setObject:idCard forKey:@"ID_card"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/updateInfo?token=",[STUserManger defult].token];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD showSuccessWithStatus:@"更改身份证号成功"];
                                                 STTableEasyModel * model = self.dataSouce[indexPath.row];
                                                 model.detailString = idCard;
                                                 [self.tableView reloadData];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end

